//
//  VisitorLoginView.swift
//  MyWeibo
//
//  Created by 余丽丽 on 15/10/9.
//  Copyright © 2015年 余丽丽. All rights reserved.
//
// 访客登录视图
import UIKit

/// 声明访客协议
protocol VisitorLoginViewDelegate: NSObjectProtocol{
    //将要登录
    func VisitorLoginViewWillLogin()
    //将要注册
    func VisitorLoginViewWillRegister()
}
class VisitorLoginView: UIView {

    weak var delegate:VisitorLoginViewDelegate?
    
    // MARK: - 设置界面
    /// 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    /// 兼容storyboard
    required init?(coder aDecoder: NSCoder) {
        // 禁止用 sb / xib 使用本类
        // fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }
    
    ///  开始动画
    private func startAnimation(){
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        animation.removedOnCompletion = false
        
        iconImageView.layer.addAnimation(animation, forKey: nil)
    
    }
    
    // MARK: - 设置视图信息
    // 原思路是:替换小房子图片,隐藏旋转图片
    // 但是有BUG:切换界面的时候,布局发生变化
    // 原因是: 其他界面要替换上来的图片尺寸跟旋转图片一致,比小房子要大
    // 解决办法:替换旋转图片,根据ishome开启动画,或者将遮挡图片移到最底层
    func setupViewInfo(isHome: Bool, imageName: String,message: String){
        iconImageView.image = UIImage(named: imageName)
        textLable.text = message
        homeIconImageView.hidden = !isHome
        isHome ? startAnimation() : sendSubviewToBack(maskIconImageView)
    
    }
    
    // MARK: - 按钮监听方法
    /// 点击注册按钮
    @objc private func clickRegisterButton(){
    
        delegate?.VisitorLoginViewWillRegister()
    
    }
    
    /// 点击登录按钮
    @objc private func clickLoginButton(){
        delegate?.VisitorLoginViewWillLogin()
        
    }

    /// 设置访客视图的UI元素
    private func setupUI(){
        // 设置背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1.0)
    
        // ***************** 使用懒加载的方式添加子控件 ***************
        addSubview(iconImageView)
        addSubview(maskIconImageView)
        addSubview(homeIconImageView)
        addSubview(textLable)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 设置子控件的约束
        setupUIConstraint()
        
    }
    
    // MARK:- 设置子控件的约束
    private func setupUIConstraint(){
    
        // 自动布局 核心公式:view1.attr1 = view2.attr2 * multiplier + constant
        // 1.旋转图标
        iconImageView.translatesAutoresizingMaskIntoConstraints = false
        // centerX与父控件对齐
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        // centerY与父控件对齐
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))
        
        // 2.遮挡视图
        maskIconImageView.translatesAutoresizingMaskIntoConstraints = false
        // centerX与iconImageView
        addConstraint(NSLayoutConstraint(item: maskIconImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        // centerY与iconImageView
        addConstraint(NSLayoutConstraint(item: maskIconImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        // 3.小房子
        homeIconImageView.translatesAutoresizingMaskIntoConstraints = false
        // centerX与iconImageView
        addConstraint(NSLayoutConstraint(item: homeIconImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        // centerY与iconImageView
        addConstraint(NSLayoutConstraint(item: homeIconImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        // 4.描述文字
        textLable.translatesAutoresizingMaskIntoConstraints = false
        // centerX与iconImageView
        addConstraint(NSLayoutConstraint(item: textLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        // top 与 小房子底部 间隔16
        addConstraint(NSLayoutConstraint(item: textLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: homeIconImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 50))
        addConstraint(NSLayoutConstraint(item: textLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
    
        // 5.注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: -10))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        // 6.登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
    }
    
    // MARK: - 懒加载控件
    // 旋转图标
    private lazy var iconImageView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return imageView
    }()
  
    // 遮挡视图
    private lazy var maskIconImageView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return imageView
        }()
   
    // 小房子图标
    private lazy var homeIconImageView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return imageView
        }()
   
    // 描述文字
    private lazy var textLable: UILabel = {
        let lable = UILabel()
        
        // 设置lable属性
        lable.text = "关注一些人，回这里看看有什么惊喜"
        lable.font = UIFont.systemFontOfSize(14)
        lable.textColor = UIColor.darkGrayColor()
        lable.textAlignment = NSTextAlignment.Center
        lable.numberOfLines = 2
        
        return lable
    }()
    
    // 注册按钮
    private lazy var registerButton: UIButton = {
        let btn = UIButton()
        
        // 设置button属性
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "clickRegisterButton", forControlEvents: UIControlEvents.TouchUpInside)

        
        return btn
    }()
    
    // 登录按钮
    private lazy var loginButton: UIButton = {
        let btn = UIButton()
        
        // 设置button属性
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "clickLoginButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return btn
        }()

}
